//
//  WelcomeLoginViewController.m
//  TenThousand
//
//  Created by 我的 on 16/6/27.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "WelcomeLoginViewController.h"
#import "LoginViewController.h"
#import "ForgotPasswordViewController.h"

@interface WelcomeLoginViewController ()
@property (weak, nonatomic) IBOutlet UIView *backView;
@property (weak, nonatomic) IBOutlet UITextField *mobileNum;
@property (weak, nonatomic) IBOutlet UITextField *passwordNum;
//@property (weak, nonatomic) IBOutlet UIButton *agreeBtn;

//@property (weak, nonatomic) IBOutlet UIButton *nextBtn;//下一步
@end

@implementation WelcomeLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"登录";
    _backView.layer.borderWidth = 1;
    _backView.layer.borderColor = [UIColor colorWithRed:0.80 green:0.80 blue:0.80 alpha:1.00].CGColor;
    
    _passwordNum.secureTextEntry = YES;
    //设置返回按钮
    [self createLeft:[UIImage imageNamed:@"ZHSZicon_03"] leftClick:^{
        [self.navigationController popViewControllerAnimated:YES];
    } right:nil rightClick:nil];
    
//     self.agreeBtn.selected = YES;//默认为选中
//    [self setViewHeight];
}
//忘记密码
- (IBAction)forgetPassword:(id)sender {
    ForgotPasswordViewController *forget = [[ForgotPasswordViewController alloc] init];
    [self setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:forget animated:YES];
}



//登录
- (IBAction)nextBtn:(id)sender {
    //判断输入的手机号码是否为合法
    
    if ([_mobileNum.text isEqualToString:@""]){
        [ToastView showInfoWithStatus:@"请输入手机号"];
    }else if (![PhoneNumStandard isStandardOrNotWithPhoneNumber:_mobileNum.text]) {
        [ToastView showInfoWithStatus:@"手机号输入不合法，请重新输入"];
    
    }else if([_passwordNum.text isEqualToString:@""]){
        [ToastView showInfoWithStatus:@"请输入密码"];
    }else{
        [self loginRequest];
    }

    
    
}

//注册
- (IBAction)registered:(id)sender {
    
     LoginViewController *login = [[LoginViewController alloc] init];
    [self setHidesBottomBarWhenPushed:YES];
    
    [self.navigationController pushViewController:login animated:YES];
}



- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


#pragma mark -----登录接口--------
- (void)loginRequest{
    
//    http://open.wanyuanfu.net/api/user/openapi  User.GetUserLogin  User.GetUserLogin
    
    /**
     *  LoginName  必选
     *  Password   必选
     */
    
    NSDictionary *para = @{@"Password":_passwordNum.text,@"LoginName":_mobileNum.text};
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"User.GetUserLogin" withParaments:para withSuccessBlock:^(NSDictionary *object) {
        
        NSLog(@"%@",object);
        if ([object[@"Code"] isEqualToString:@"00000"]) {
            
            [[NSUserDefaults  standardUserDefaults] setValue:_mobileNum.text forKey:@"LoginName"];//用户名
            [[NSUserDefaults  standardUserDefaults] setValue:_passwordNum.text forKey:@"Password"];//密码
            [[NSUserDefaults  standardUserDefaults] setValue:object[@"Data"][0][@"LoginId"] forKey:@"LoginId"];//id
            
            
            [ToastView showInfoWithStatus:object[@"Message"]];
            [self.navigationController popViewControllerAnimated:YES];
        }
        [ToastView showInfoWithStatus:object[@"Message"]];
       
    } withFailureBlock:^(NSError *error) {
        NSLog(@"%@",error);
    } progress:nil];
   
}


@end
